package com.sys.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dto.sys.EmployeeDto;
import com.sys.EmployeeEvent;
import com.sys.dao.EmployeeMapper;
import com.sys.service.EmployeeService;
import com.utils.result.Ret;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xufan
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public Ret listPageEmployee(EmployeeDto employeeDto) {
        Integer pageNumber = Integer.parseInt(employeeDto.getPageNumber());
        Integer pageSize = Integer.parseInt(employeeDto.getPageSize());
        Page<Map<String, Object>> pageObject = new Page<>(pageNumber, pageSize);
        IPage<Map> listEmployee = employeeMapper.listPageEmployee(employeeDto, pageObject);
        return Ret.success(listEmployee);
    }

    @Override
    public Ret getEmployeeById(EmployeeDto employeeDto) {
        return Ret.success(employeeMapper.getEmployeeById(employeeDto));
    }

    @Override
    public Ret saveEmployee(EmployeeDto employeeDto) {
        employeeMapper.saveEmployee(employeeDto);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("employeeName", employeeDto.getEmployeeName());
        param.put("employeeId", employeeDto.getEmployeeId());
        //开启异步事件
        applicationEventPublisher.publishEvent(new EmployeeEvent(param));
        return Ret.success(employeeDto.getEmployeeId());
    }
}
